package com.xiaoyao.novel.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.collections.MapUtils;

import com.xiaoyao.novel.dao.BookDao;
import com.xiaoyao.novel.pojo.Book;

public class BookService extends BaseService<Book> {

	static BookDao dao = new BookDao();
	public BookService() {
		super(dao);
	}
	
	static final  boolean USER_CACHE =true ; 
	static Map<Integer, Book> bookCacheMap=new HashMap<Integer, Book>();
	 
	/**
	 * 获取非完成书籍
	 */
	@Override
	public List<Book> getAll() {
		return super.getAll();
	} 

	/**
	 * 获取所有书籍.包括已完成书籍
	 * @return
	 */
	public List<Book> getAllBook() {
		List<Book> bookList = new ArrayList<Book>();
		try {
			bookList = dao.getAllBook();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bookList;
	}
	@Override
	public Book getById(Object id) {
		if (MapUtils.isEmpty(bookCacheMap )) {
			List<Book> bookList=new ArrayList<Book>();
			try {
				bookList = dao.getAllBook();
			} catch (SQLException e) {
				logger.error(e);
			}
			for(Book book:bookList){
				bookCacheMap.put(book.bookId, book) ;
			}
		}
		Book book =bookCacheMap.get(id);
		if (book==null) {
			book = super.getById(id);
			if (book!=null) {
				bookCacheMap.put(book.bookId, book) ; 
			}
		} 
		return book;
	}

	public void calcClick(int bookId, int addClick) {
		try {
			Map<String, Integer> map = new HashMap<String, Integer>();
			map.put("bookId", bookId);
			map.put("addClick", addClick);
			dao.calcClick(map);
			
			bookCacheMap.remove(bookId);
		} catch (Exception e) {
			logger.error(e);
		}
	} 
	public void calcClick(Map<Integer, Integer> clickCountMap) {
		try {
			List<Map<String, Integer>> mapList = new ArrayList<Map<String, Integer>>();
			for (Entry<Integer, Integer> entry : clickCountMap.entrySet()) {
				Map<String, Integer> map = new HashMap<String, Integer>();
				map.put("bookId", entry.getKey());
				map.put("addClick", entry.getValue());
				mapList.add(map);

				bookCacheMap.remove( entry.getKey());
			}
			dao.calcClick(mapList);
		} catch (Exception e) {
			logger.error(e);
		}
	}

	@Override
	public void update(Book book) {
		bookCacheMap.remove(book.bookId);
		super.update(book);
	} 
	public void updateCrawl(Book book) {
		try {
			dao.updateCrawl(book);
			bookCacheMap.remove(book.bookId);
		} catch (Exception e) {
			logger.error(e);
		}
	}
	public static void main(String[] args) {
		BookService bookService = new BookService();
		Map<Integer, Integer> clickCountMap = new HashMap<Integer, Integer>();
		clickCountMap.put(111, 1);
		clickCountMap.put(90, 1);
		clickCountMap.put(91, 1);
		clickCountMap.put(92, 1);
		clickCountMap.put(93, 1);
		clickCountMap.put(94, 1);
		clickCountMap.put(98, 1);
		clickCountMap.put(97, 1);
		clickCountMap.put(96, 1);
		clickCountMap.put(95, 1);
		long start = System.currentTimeMillis();
//		bookService.calcClick(clickCountMap);
		bookService.getById(39);
		bookService.getById(39);
		
		long end = System.currentTimeMillis();
		long spend = end - start;
		System.out.println("批量耗时:" + spend);
//		start = System.currentTimeMillis();
//		for (Entry<Integer, Integer> entry : clickCountMap.entrySet()) {
//			bookService.calcClick(entry.getKey(), entry.getValue());
//		}
//		end = System.currentTimeMillis();
//		spend = end - start;
//		System.out.println("单个耗时:" + spend);

	}

}
